<?php
/**
 * 
 * Abstract content node model
 * 
 * @category Abovo
 * 
 * @package Abovo_Model
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 * @license http://opensource.org/licenses/bsd-license.php BSD
 * 
 */

/**
 * 
 * Abstract content node model
 * 
 * @category Abovo
 * 
 * @package Abovo_Model
 * 
 */
class Abovo_Model_Wikis extends Abovo_Sql_Table {
    
    /**
     * 
     * Tagging model
     * 
     * @var Abovo_Model_Tag
     * 
     */
    protected $_tags;
    
    /**
     * 
     * undocumented class variable
     * 
     * @var string
     * 
     */
    private $_area = 'wiki';
    
    /**
     * 
     * undocumented function
     * 
     * @return void
     * 
     */
    public function fetchByName($name)
    {
        $wiki = $this->select('row', array('name = ?' => $name));
        
        if (count($wiki) === 0) {
            return false;
        }
        
        $wiki->tags = $this->_tags->fetchAll($wiki->id);
        return $wiki;
    }
    
    /**
     * 
     * Saves or updates one row by it's id
     *
     * @return void
     * 
     */
    public function save($data)
    {
        $saved = parent::save($data);
        
        // if tags given, then refresh them
        if (isset($data['tags'])) {
            $saved['tags'] = $this->_tags->refresh(
                $data['tags'],
                $saved['id'],
                $saved['handle']
            );
        }
        
        // return inserted/updated data
        return $saved;
    }
    
    /**
     * 
     * Gets a form for wiki page entry
     * 
     * @return void
     * 
     */
    public function form($data = array())
    {
        $form = Solar::factory('Abovo_Form');
        
        $form->load(
            'Abovo_Form_Load_Table',
            $this,
            array('body'),
            'wiki'
        );
        
        // add element for tags
        $form->setElement('wiki[tags]', array(
            'type'    => 'text',
            'label'   => $this->locale('TEXT_TAGS', 2),
            'attribs' => array(
                'size' => 48,
            )
        ));
        
        // add submit button
        $form->setElement('process', array(
            'type'  => 'submit',
            'value' => $this->locale('PROCESS_SAVE'),
        ));
        
        $form->elements['wiki[body]']['attribs']['cols'] = 80;
        $form->elements['wiki[body]']['attribs']['rows'] = 20;
        
        return $form;
    }
    /**
     * 
     * Table setup
     * 
     * @return void
     * 
     */
    protected function _setUp()
    {
        $this->_name = 'wikis';
        
        $this->_col['created'] = array(
            'type'     => 'timestamp',
            'require'  => true,
        );
        
        $this->_col['updated'] = array(
            'type'     => 'timestamp',
            'require'  => true,
        );
        
        $this->_col['handle'] = array(
            'type'    => 'varchar',
            'size'    => 15,
            'require' => true,
        );
        
        $this->_col['name'] = array(
            'type'    => 'varchar',
            'size'    => 50,
            'require' => true,
        );
        
        $this->_col['body'] = array(
            'type'    => 'clob',
            'require' => true,
        );
        
        $this->_col['ip'] = array(
            'type'    => 'varchar',
            'size'    => 15,
            'require' => true,
            'valid'   => 'ipv4',
        );
        
        // Make sure sql is available
        if (! Solar::isRegistered('sql')) {
            Solar::register('sql', Solar::factory('Solar_Sql'));
        }
        
        // instantiate tagging model
        $tag_config = array('area' => $this->_area);
        $this->_tags = Solar::factory('Abovo_Model_Tag', $tag_config);
    }
}